//
//  FeedBackController.m
//  QuickCure
//
//  Created by MackBook on 16/9/12.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "FeedBackController.h"
#import <Masonry.h>
#import <SVProgressHUD.h>

#define textViewCount _numberLabel.text = [NSString stringWithFormat:@"%d",200 - (int)_textView.text.length];


@interface FeedBackController () <UITableViewDataSource,UITableViewDelegate,UITextViewDelegate>

//反馈信息的view

@property (weak,nonatomic)UIView *messageView;

//客服电话的tableview

@property (weak,nonatomic)UITableView *callTableView;

//显示字数的label

@property (weak,nonatomic)UILabel *numberLabel;

//提交反馈的按钮

@property (weak,nonatomic)UIButton *submitButton;

//显示反馈的textView

@property (weak,nonatomic)UITextView *textView;

@end

@implementation FeedBackController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self creatFeedBack];
    
    [self setupInforViews];
    
    [self realizeInForView];
    
}

- (void)creatFeedBack{
    
#pragma mark -- 创建view
    
    UIView *view = [[UIView alloc]init];
    
    self.messageView = view;
    
    [self.view addSubview:view];
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.leading.trailing.equalTo(self.view).offset(0);
        make.height.mas_equalTo(self.view).multipliedBy(0.6);
        
    }];
    
    UITableView *tableView = [[UITableView alloc]init];
    
    self.callTableView = tableView;
    
    [self.view addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.leading.trailing.equalTo(self.view).offset(0);
        make.height.mas_equalTo(self.view).multipliedBy(0.4);
        
    }];
}

#pragma mark -- 完善创建的view

- (void)setupInforViews{
    
    self.callTableView.dataSource = self;
    
    self.callTableView.delegate = self;
    
    self.callTableView.tableFooterView = [UIView new];
    
    UITextView *textView = [[UITextView alloc]init];
    
#warning -- 三原色要除以255必须加.0不+的话就是黑色,因为是float类型,不+.0永远是int类型
    
    textView.backgroundColor = [UIColor colorWithRed:128/255.0 green:128/255.0 blue:128/255.0 alpha:0.1];
    
    self.textView = textView;
    
    [self.view addSubview:textView];
    
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.messageView).insets(UIEdgeInsetsMake(75, 10, 120, 10));
        
    }];
    
    UIButton *submitBtn = [[UIButton alloc]init];
    
    UIImage *image = [[UIImage imageNamed:@"dijiaofankui"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    [submitBtn setBackgroundImage:image forState:UIControlStateNormal];
    
    self.submitButton = submitBtn;
    
    [submitBtn addTarget:self action:@selector(emptyButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:submitBtn];
    
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.trailing.equalTo(self.view).offset(-10);
        make.bottom.equalTo(textView).offset(50);
        make.width.equalTo(@89.7);
        make.height.equalTo(@40.3);
    }];
    
    UILabel *numberLabel = [[UILabel alloc]init];
    
    numberLabel.text = @"200";
    
    self.numberLabel = numberLabel;
    
    [self.view addSubview:numberLabel];
    
    [numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(textView.mas_bottom).offset(15);
        make.right.equalTo(submitBtn.mas_left).offset(-20);
        
    }];
    
    UILabel *textLabel = [[UILabel alloc]init];
    
    textLabel.text = @"您还可以输入的字数为:";
    
    [self.view addSubview:textLabel];
    
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(numberLabel);
        make.right.equalTo(numberLabel.mas_left).offset(-15);
    }];
    
    UIView *weView = [[UIView alloc]init];
    
    weView.backgroundColor = [UIColor colorWithRed:128/255.0 green:128/255.0 blue:128/255.0 alpha:0.1];
    
    [self.view addSubview:weView];
    
    [weView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(submitBtn.mas_bottom).offset(10);
        make.bottom.equalTo(self.messageView.mas_bottom);
        make.width.equalTo(self.messageView.mas_width);
        
    }];
    
    UILabel *wecomeLabel = [[UILabel alloc]init];
    
    wecomeLabel.text = @"欢迎联系我们";
    
    [self.view addSubview:wecomeLabel];
    
    [wecomeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(weView);
        make.leading.equalTo(weView).offset(15);
    }];
    
}

#pragma mark -- 客服电话的tableview的数据源方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    
    if(indexPath.row == 0){
        
        cell.textLabel.text = @"客服电话: 400-636-6113";
        
        cell.imageView.image = [UIImage imageNamed:@"dianhua"];
        
    }
    else if (indexPath.row == 1){
        
        cell.textLabel.text = @"用户交流群: QQ464768569";
        
        cell.imageView.image = [UIImage imageNamed:@"qq"];
    }
    else if(indexPath.row == 2){
        
        cell.textLabel.text = @"123";
    }
    
    return cell;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    if (self.textView.text.length == 0) {
        
        self.textView.text = @"请输入反馈的信息.";
        
        self.textView.font = [UIFont systemFontOfSize:19];
        
        self.textView.textColor = [UIColor colorWithRed:128/255.0 green:128/255.0 blue:128/255.0 alpha:1];
    }
    
    [self.view endEditing:YES];
}

#warning -- BUG:cell下划线缺失,严重影响美观.NONONO不是BUG所有的APP都是这样的,与使用系统的cell无关
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.row == 0){
        
        UIAlertController *alterController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否拨打电话:400-636-6113" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            //取消响应者
            [self.view endEditing:YES];
            
        }];
        
        [alterController addAction:cancelAction];
        
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            UIApplication *application = [UIApplication sharedApplication];
            NSURL *url = [NSURL URLWithString:@"tel://400-636-6113"];
            [application openURL:url];
            //取消响应者
            [self.view endEditing:YES];
        }];
        
        [alterController addAction:sureAction];
        
        [self presentViewController:alterController animated:YES completion:nil];
    }
}

#pragma mark -- 实现输入信息改变文本与按钮的事件
- (void)realizeInForView{
    
    self.textView.text = @"请输入反馈的信息.";
    
    self.textView.font = [UIFont systemFontOfSize:19];
    
    self.textView.textColor = [UIColor colorWithRed:128/255.0 green:128/255.0 blue:128/255.0 alpha:1];
    
    //设置文本textView的代理对象,通过代理方法修改内容
    
    self.textView.delegate = self;
}

#pragma markk -- 实现textView代理方法

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    
    self.textView.text = @"";
    
    self.textView.textColor = [UIColor blackColor];
    
    //关闭联想功能与首字母大写的功能
    
    [self.textView setAutocorrectionType:UITextAutocorrectionTypeNo];
    
    [self.textView setAutocapitalizationType:UITextAutocapitalizationTypeNone];

    return YES;
}

#pragma mark -- textView代理方法:输入的时候就会调用

- (void)textViewDidChange:(UITextView *)textView{
    
    NSString *string = [NSString stringWithFormat:@"%@",self.textView.text];
    
    //修改numLabel的值
    
    [self.numberLabel setText:[NSString stringWithFormat:@"%lu",200-textView.text.length]];
    
    //当文本输入超过预计字符时显示为0,禁用textView
    
    if(string.length > 200){
        
        [self.numberLabel setText:[NSString stringWithFormat:@"0"]];
        
        [self.textView setEditable:NO];
    }
}

- (void)emptyButtonClick{
    
    if(self.textView.text.length == 9 || self.textView.text.length == 0 ){
        
        [SVProgressHUD showErrorWithStatus:@"没有写反馈就想提交?"];
        
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleLight];
        
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeGradient];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD dismiss];
        });
        
    }
    else if (self.textView.text.length > 0){
        
        [self.navigationController popViewControllerAnimated:YES];
        
        [SVProgressHUD showSuccessWithStatus:@"反馈意见已提交"];
        
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleLight];
        
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeCustom];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD dismiss];
        });
    }
    
    
}

@end
